package exer;

import java.util.ArrayList;
import java.util.List;

class Solution {
    static TreeNode root;
    public static void main(String[] args) {
        Solution solution = new Solution();
        solution.create();
        solution.preorderTraversal(root);

    }
    public void create () {
        // 先所有的节点创建出来
        TreeNode node1 = new TreeNode(1);
        TreeNode node2 = new TreeNode(2);
        TreeNode node3 = new TreeNode(3);
        TreeNode node4 = new TreeNode(4);
        TreeNode node5 = new TreeNode(5);
        TreeNode node6 = new TreeNode(6);
        TreeNode node7 = new TreeNode(7);
        TreeNode node8 = new TreeNode(8);
        // 处理引用关系
        node1.left = node2;
        node1.right = node3;
        node2.left = node4;
        node2.right = node5;
        node5.right = node8;
        node3.left = node6;
        node3.right = node7;
        // 指定根节点的引用
        root = node1;
    }
    public List<Integer> preorderTraversal(TreeNode root) {
        if(root==null){
            return null;
        }
        List<Integer>list=new ArrayList<>();
        list.add(root.val);
        list.addAll(preorderTraversal(root.left));
        list.addAll(preorderTraversal(root.right));
        return list;
    }
}
class TreeNode {
     int val;
     TreeNode left;

     TreeNode right;

     TreeNode() {}
     TreeNode(int val) { this.val = val; }
     TreeNode(int val, TreeNode left, TreeNode right) {

         this.val = val;
         this.left = left;
         this.right = right;
     }
 }